package com.jar.exercise.网络编程.UDP连接.Demo3;


import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * @author 松鼠
 * @date 2019/6/30 -  18:56
 */
public class Server {
	public static void main(String[] args) {
		try (DatagramSocket datagramSocket = new DatagramSocket(8000)) {
			//创建一个字节数组
			byte[] b = new byte[1024];
			//创建一个空包
			DatagramPacket datagramPacket = new DatagramPacket(b, b.length);
			while (true) {
				//接收
				datagramSocket.receive(datagramPacket);
				//接收的数据
				byte[] data = datagramPacket.getData();
				System.out.println("收到的消息为:" + new String(data, 0, datagramPacket.getLength()));
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
